package io.geekidea.springbootplus.system.controller;

import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.entity.SysAddress;
import io.geekidea.springbootplus.system.entity.SysMember;
import io.geekidea.springbootplus.system.param.SysAddressPageParam;
import io.geekidea.springbootplus.system.param.sysmember.SysMemberPageParam;
import io.geekidea.springbootplus.system.service.SysAddressService;
import io.geekidea.springbootplus.system.vo.SysAddressQueryVo;
import io.geekidea.springbootplus.system.vo.SysMemberQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sysAddress")
@Module("system")
@Api(value = "收货地址API", tags = {"收货地址"})
public class SysAddressController extends BaseController {

    @Resource
    private SysAddressService sysAddressService;

    @PostMapping("/add")
    @OperationLog(name = "收货地址", type = OperationLogType.INFO)
    @ApiOperation(value = "(前端)添加收货地址", response = SysAddress.class)
    public ApiResult<String> addSysAddress(@RequestBody SysAddress sysAddress)throws Exception {
        boolean result = sysAddressService.addAddress(sysAddress);
        if (result){
            return ApiResult.ok("地址添加成功！");
        }else {
            return ApiResult.fail(500,"地址添加失败！");
        }
    }

    @GetMapping("/delete/{id}")
    @OperationLog(name = "收货地址", type = OperationLogType.INFO)
    @ApiOperation(value = "(前端)删除收货地址", response = SysAddress.class)
    public ApiResult<String> deleteAddress (@PathVariable("id") Integer id )throws Exception{
        boolean result = sysAddressService.deleteAddress(id);
        if (result){
            return ApiResult.ok("删除地址成功！");
        }else {
            return ApiResult.fail(500,"删除地址失败！");
        }
    }

    @PostMapping("/update")
    @OperationLog(name = "收货地址", type = OperationLogType.INFO)
    @ApiOperation(value = "(前端)修改收货地址", response = SysAddress.class)
    public ApiResult<String> updateSysAddress(@RequestBody SysAddress sysAddress)throws Exception {
        boolean result = sysAddressService.updateAddress(sysAddress);
        if (result){
            return ApiResult.ok("地址修改成功！");
        }else {
            return ApiResult.fail(500,"地址修改失败！");
        }
    }

    @PostMapping("/deleteBatchIds")
    @OperationLog(name = "批量删除收货地址", type = OperationLogType.PAGE)
    @ApiOperation(value = "(前端)批量删除收货地址", response = SysAddress.class)
    public ApiResult<String>  deleteBatchIds(@RequestBody List<Integer> ids )throws Exception {
        System.out.println(ids);
        boolean result = sysAddressService.deleteAddressBatchIds(ids);
        if (result){
            return ApiResult.ok("批量删除成功！");
        }else {
            return ApiResult.fail(500,"批量删除失败！");
        }
    }

    @GetMapping("/selectById/{id}")
    @OperationLog(name = "查询收货地址", type = OperationLogType.INFO)
    @ApiOperation(value = "(前后端)根据Id查询收货地址", response = SysAddress.class)
    public ApiResult getAddressById (@PathVariable("id") Integer id )throws Exception{
        SysAddressQueryVo sysAddress = sysAddressService.selectAddressById(id);
        if (sysAddress !=null ){
            return ApiResult.ok(sysAddress);
        }else {
            return ApiResult.fail(500,"该会员没有收货地址！");
        }
    }

    /**
     * 收货地址分页列表
     */
    @PostMapping("/getPageList")
    @OperationLog(name = "收货地址列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "(后端)收货地址列表", response = SysAddressQueryVo.class)
    public ApiResult<Paging<SysAddressQueryVo>> getSysAddressPageList(@Validated @RequestBody SysAddressPageParam sysAddressPageParams) throws Exception {
        Paging<SysAddressQueryVo> paging = sysAddressService.getSysAddressPageList(sysAddressPageParams);
        return ApiResult.ok(paging);
    }

    /**
     * 根据会员编号查询收货地址
     */
    @GetMapping("/selectAddressByMemberId/{memberId}")
    @OperationLog(name = "查询收货地址列表", type = OperationLogType.INFO)
    @ApiOperation(value = "(前端)根据会员编号查询收货地址", response = SysAddress.class)
    public ApiResult getAddressByMemberId (@PathVariable("memberId") Integer memberId )throws Exception{
        List<SysAddressQueryVo> addressList = sysAddressService.getAddressListByMemberId(memberId);
        if (addressList.size() > 0 ){
            return ApiResult.ok(addressList);
        }else {
            return ApiResult.fail(500,"该会员没有收货地址！");
        }
    }

    /*
    * 修改默认地址
    * */
    @PostMapping("/updateDefaultAddress")
    @OperationLog(name = "收货地址", type = OperationLogType.INFO)
    @ApiOperation(value = "(前端)修改默认收货地址", response = SysAddress.class)
    public ApiResult<String> updateDefaultAddress(@RequestBody SysAddress sysAddress)throws Exception {
        boolean result = sysAddressService.updateDefaultAddress(sysAddress.getMemberId(), sysAddress.getId());
        if (result){
            return ApiResult.ok("修改默认地址成功！");
        }else {
            return ApiResult.fail(500,"修改默认地址失败！");
        }
    }
}
